class HtmlParser:
    def __init__(self) -> None:
        None

    def parse_html(self, html, title):
        title_index = html.index("<h4>" + title + "</h4>")
        end_index = html.index("]]}]", title_index) + 4
        series_index = html.index("series: [{", title_index, end_index)
        return html[series_index+8:end_index]

    def parse_monthly_html(self, html, title):
        title_index = html.index("<h3>" + title + "</h3>")
        series_index = html.index("series: [{", title_index)
        start_index, end_index = self.find_double_(html[series_index:], '[', ']')
        return html[series_index+start_index:series_index+end_index]

    def find_double_(self, str, left, right):
        start = False
        left_count = 0
        start_index = 0
        end_index = 0
        for i in range(len(str)):
            ch = str[i]
            if ch == left:
                # 刚找到left，获取left的索引
                if start == False:
                    start_index = i
                # 设置查找开始
                start = True
                # left计算加一
                left_count = left_count + 1
            if ch == right:
                # 找到右侧且查找已开始，直到left_count为0结束查找
                if start == True:
                    left_count = left_count - 1
                    if left_count == 0:
                        end_index = i
                        break
        # end_index需要加一截取出来的才是对的
        return start_index, end_index+1


if __name__ == '__main__':
    parser = HtmlParser()
    str = "series: [{" + \
          " name: 'Average High Temp (°c)', color: '#FF8533'," + \
          " data: [18, 20, 22, 26, 28, 30, 31, 32, 30, 27, 24, 19]}," + \
          "{" + \
          " name: 'Average Low Temp (°c)', color: '#4572A7'," + \
          " data: [10, 11, 14, 17, 20, 22, 23, 23, 22, 19, 16, 11]}" + \
          "]"
    start_index, end_index = parser.find_double_(str, '[', ']')

    index_ = str[start_index:end_index ]
    print(index_)
